<script>
import NewCard from "@/components/NewCard.vue";
import httpInstance from "@/utils/http";
export default {
  components: {NewCard},
  data() {
    const userInfo = JSON.parse(localStorage.getItem('user')) || {
      nickname: '未知用户',
      avatar: 'https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png',
      signature: '这个人很懒，什么都没留下'
    };
    return {
      textarea: '',
      userInfo: userInfo,
      activeCommentId: null,
      commentText: '',
      replyingTo: null,
      newsList: [],
      replyText: '',
      newsCount: 0
    }
  },
  methods: {
    toggleComments(newsId) {
      if (this.activeCommentId === newsId) {
        this.activeCommentId = null;
      } else {
        this.activeCommentId = newsId;
      }
      this.commentText = '';
    },
    // 获得动态
    async fetchNews() {
      console.log('开始获取动态列表');
      try {
        console.log('发送请求: GET /api/news/getByCurUser');
        const response = await httpInstance.get('/api/news/getByCurUser');
        console.log('获取动态响应数据:', response);
        
        // 检查响应是否包含code和data字段
        if (response && response.code === 200 && Array.isArray(response.data)) {
          console.log('响应是标准对象格式，包含data数组');
          // 按时间排序，最新的在前面
          this.newsList = this.sortNewsByTime(response.data);
          console.log('动态列表更新成功，条数:', this.newsList.length);
        } 
        // 检查响应本身是否为数组
        else if (Array.isArray(response)) {
          console.log('响应本身是数组格式，直接使用');
          // 按时间排序，最新的在前面
          this.newsList = this.sortNewsByTime(response);
          console.log('动态列表更新成功，条数:', this.newsList.length);
        } 
        // 其他情况
        else {
          console.error('获取动态失败，响应格式不符合预期:', response);
          this.$message.error('获取动态失败，响应格式不符合预期');
        }
      } catch (error) {
        console.error('获取动态请求异常:', error);
        this.$message.error('获取动态失败，请稍后重试');
      }
    },
    
    // 按时间排序动态列表，最新的在前面
    sortNewsByTime(newsList) {
      return [...newsList].sort((a, b) => {
        const timeA = new Date(a.createTime || 0).getTime();
        const timeB = new Date(b.createTime || 0).getTime();
        return timeB - timeA; // 降序排列，最新的在前面
      });
    },
    
    async publishNews() {
      console.log('开始发布动态');
      if (!this.textarea.trim()) {
        console.warn('动态内容为空，取消发布');
        this.$message.warning('请输入动态内容');
        return;
      }
      
      try {
        const newsData = {
          nickname: this.userInfo.nickname,
          msg: this.textarea,
          likes: 0
        }
        console.log('发布动态数据:', newsData);
        console.log('发送请求: POST /api/news/push');
        const response = await httpInstance.post('/api/news/push', newsData);
        console.log('发布动态响应数据类型:', typeof response);
        console.log('发布动态响应数据:', response);
        
        if (response && response.code === 200) {
          this.$message.success(response.msg || '发布成功');
          this.textarea = '';
          this.fetchNews();
        } else {
          console.error('发布动态失败，错误信息:', response?.msg);
          this.$message.error(response?.msg || '发布失败');
        }
      } catch (error) {
        console.error('发布动态请求异常:', error);
        this.$message.error('发布失败，请稍后重试');
      }
    },
    
    likeNews(newsId) {
      console.log(`点赞动态ID=${newsId}`);
      try {
        console.log(`发送请求: POST /api/news/like/${newsId}`);
        httpInstance.post(`/api/news/like/${newsId}`).then((response) => {
          console.log('点赞动态响应数据:', response);
          if (response && response.code === 200) {
            const news = this.newsList.find(item => item.id == newsId);
            if (news) {
              news.likes++;
              console.log(`动态点赞成功，当前点赞数:${news.likes}`);
            }
          } else {
            console.error('点赞动态失败，错误信息:', response?.msg);
          }
        });
      } catch (error) {
        console.error('点赞动态请求异常:', error);
      }
    },
    
    // 获取当前用户动态数量
    async fetchNewsCount() {
      console.log('开始获取用户动态数量');
      try {
        console.log('发送请求: GET /api/news/num');
        const response = await httpInstance.get('/api/news/num');
        console.log('获取动态数量响应数据:', response);
        
        if (response && response.code === 200) {
          this.newsCount = response.data || 0;
          console.log('动态数量更新成功:', this.newsCount);
        } else {
          console.error('获取动态数量失败，错误信息:', response?.msg);
          this.newsCount = 0;
        }
      } catch (error) {
        console.error('获取动态数量请求异常:', error);
        this.newsCount = 0;
      }
    }
  },
  created() {
    console.log('NewsView 组件创建');
    console.log('当前用户信息:', this.userInfo);
    this.fetchNews();
    this.fetchNewsCount();
  }
}
</script>

<template>
  <div class="news_box">
    <div class="left">
      <div class="user-info">
        <el-avatar :size="60" :src="userInfo.avatar"></el-avatar>
        <div class="info-text">
          <span class="username">{{ userInfo.nickname }}</span>
          <span class="signature">学号：{{ userInfo.sno }}</span>
        </div>
      </div>
      <div class="stats">
        <div class="stat-item">
          <span class="stat-number">{{ newsCount }}</span>
          <span class="stat-label">动态</span>
        </div>
      </div>
      
      <div class="user-activity">
        <div class="section-title">
          <i class="el-icon-data-analysis"></i>
          <span>活动日历</span>
        </div>
        <div class="calendar-preview">
          <div class="calendar-day" v-for="day in 7" :key="day">
            <div class="day-circle" :class="{'active': day === 3 || day === 5}">
              {{ day }}
            </div>
          </div>
        </div>
        <div class="activity-info">本周活跃度: <span class="highlight">良好</span></div>
      </div>
    </div>

    <div class="main">
      <div class="input_box">
        <el-container>
          <el-header height="20px">
            <i class="el-icon-edit"></i> 发表动态
          </el-header>
          <el-main>
            <el-input
              type="textarea"
              :rows="4"
              placeholder="分享新鲜事..."
              v-model="textarea">
            </el-input>
          </el-main>
          <el-footer>
            <div class="input_box_left">
              <i class="el-icon-picture-outline"></i>
              <i class="el-icon-video-camera"></i>
              <i class="el-icon-link"></i>
            </div>
            <div class="input_box_right">
              <el-button type="primary" icon="el-icon-s-promotion" @click="publishNews">发布</el-button>
            </div>
          </el-footer>
        </el-container>
      </div>

      <div class="content">
        <new-card v-for="o in newsList" :key="o.id" :item="o"></new-card>
      </div>
    </div>

    <div class="right">
      <div class="system-notice">
        <div class="section-title">
          <i class="el-icon-bell"></i>
          <span>系统公告</span>
        </div>
        <div class="notice-content">
          <p>欢迎使用社团管理系统！</p>
          <p>新版本已上线，体验更流畅！</p>
          <div class="notice-time">2023-06-15</div>
        </div>
      </div>
      
      <div class="hot-topics">
        <div class="hot-topics-header">
          <i class="el-icon-hot-water"></i>
          <span>热门话题</span>
        </div>
        <ul class="topic-list">
          <li><span class="topic-tag">#</span>计算机协会招新</li>
          <li><span class="topic-tag">#</span>校园文化节</li>
          <li><span class="topic-tag">#</span>期末复习资料分享</li>
          <li><span class="topic-tag">#</span>校园歌手大赛</li>
          <li><span class="topic-tag">#</span>社团管理系统上线啦</li>
        </ul>
      </div>
      
      <div class="recommended-users">
        <div class="section-title">
          <i class="el-icon-user"></i>
          <span>推荐关注</span>
        </div>
        <div class="user-list">
          <div class="user-item" v-for="i in 3" :key="i">
            <el-avatar size="small" src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png"></el-avatar>
            <div class="user-info-mini">
              <div class="user-name">用户{{ i }}</div>
              <div class="user-desc">活跃用户</div>
            </div>
            <el-button size="mini" type="primary" round>关注</el-button>
          </div>
        </div>
      </div>
    </div>

    <el-backtop :bottom="100">
      <div class="backtop-inner">
        <i class="el-icon-top"></i>
      </div>
    </el-backtop>
  </div>
</template>

<style scoped>
.news_box {
  width: 90%;
  display: flex;
  min-height: calc(100vh - 80px);
  margin: 70px auto;
  padding: 0 20px;
  gap: 20px;
  position: relative;
  background-color: #f9fafc;
}

.left {
  width: 300px;
  height: fit-content;
  padding: 25px;
  margin-top: 0;
  margin-right: 0;
  margin-left: 0;
  background-color: white;
  border-radius: 16px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  position: sticky;
  top: 90px;
  transition: all 0.3s ease;
  border: 1px solid rgba(230, 235, 245, 0.8);
}

.left:hover {
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.12);
}

.user-info {
  display: flex;
  align-items: center;
  padding: 0;
  margin-bottom: 25px;
  gap: 15px;
}

.user-info .el-avatar {
  border: 2px solid #fff;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: transform 0.3s ease;
}

.user-info .el-avatar:hover {
  transform: scale(1.05);
}

.info-text {
  flex: 1;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.username {
  font-size: 18px;
  font-weight: 600;
  color: #333;
}

.signature {
  font-size: 14px;
  color: #666;
  line-height: 1.5;
}

.stats {
  padding: 15px 0;
  border-top: 1px solid rgba(230, 235, 245, 0.8);
  margin-top: 10px;
}

.stat-item {
  text-align: center;
  background: linear-gradient(135deg, rgba(75, 108, 183, 0.1) 0%, rgba(24, 40, 72, 0.1) 100%);
  padding: 15px;
  border-radius: 12px;
  transition: all 0.3s ease;
}

.stat-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 8px 15px rgba(75, 108, 183, 0.1);
}

.stat-number {
  font-size: 24px;
  font-weight: 600;
  color: #4b6cb7;
  display: block;
  margin-bottom: 6px;
}

.stat-label {
  font-size: 14px;
  color: #666;
  font-weight: 500;
}

.main {
  flex: 1;
  max-width: 800px;
  margin: 0 auto;
}

.input_box {
  background: white;
  border-radius: 16px;
  padding: 25px;
  margin-bottom: 25px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(230, 235, 245, 0.8);
  transition: all 0.3s ease;
}

.input_box:hover {
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.12);
}

.el-header {
  padding: 0 0 15px 0;
  font-size: 20px;
  font-weight: 600;
  color: #4b6cb7;
  display: flex;
  align-items: center;
  border-bottom: 1px solid rgba(230, 235, 245, 0.8);
  margin-bottom: 20px;
}

.el-header i {
  margin-right: 8px;
  color: #4b6cb7;
}

.el-main {
  padding: 0;
}

.el-textarea {
  margin-bottom: 20px;
}

:deep(.el-textarea__inner) {
  border-radius: 12px;
  padding: 15px;
  min-height: 100px !important;
  resize: none;
  border-color: rgba(230, 235, 245, 0.8);
  transition: all 0.3s ease;
  font-size: 15px;
}

:deep(.el-textarea__inner:focus) {
  border-color: #4b6cb7;
  box-shadow: 0 0 0 2px rgba(75, 108, 183, 0.1);
}

.el-footer {
  padding: 15px 0 0 0;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-top: 1px solid rgba(230, 235, 245, 0.8);
}

.input_box_left {
  display: flex;
  gap: 20px;
}

.input_box_left i {
  color: #666;
  font-size: 20px;
  cursor: pointer;
  transition: all 0.3s ease;
}

.input_box_left i:hover {
  color: #4b6cb7;
  transform: scale(1.1);
}

.input_box_right .el-button {
  padding: 12px 25px;
  border-radius: 25px;
  font-weight: 500;
  background: linear-gradient(135deg, #4b6cb7 0%, #182848 100%);
  border: none;
  transition: all 0.3s ease;
  box-shadow: 0 8px 15px rgba(75, 108, 183, 0.2);
}

.input_box_right .el-button:hover {
  transform: translateY(-3px);
  box-shadow: 0 12px 20px rgba(75, 108, 183, 0.3);
}

.input_box_right .el-button i {
  margin-right: 5px;
}

.content {
  margin-bottom: 40px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.right {
  width: 300px;
  position: sticky;
  top: 90px;
  height: fit-content;
}

.hot-topics {
  background: white;
  border-radius: 16px;
  padding: 25px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(230, 235, 245, 0.8);
  transition: all 0.3s ease;
}

.hot-topics:hover {
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.12);
}

.hot-topics-header {
  font-size: 18px;
  font-weight: 600;
  color: #4b6cb7;
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  padding-bottom: 15px;
  border-bottom: 1px solid rgba(230, 235, 245, 0.8);
}

.hot-topics-header i {
  margin-right: 8px;
  color: #4b6cb7;
}

.topic-list {
  list-style: none;
  padding: 0;
  margin: 0;
}

.topic-list li {
  padding: 12px 0;
  border-bottom: 1px solid rgba(230, 235, 245, 0.8);
  font-size: 15px;
  color: #555;
  cursor: pointer;
  transition: all 0.3s ease;
  display: flex;
  align-items: center;
}

.topic-list li:last-child {
  border-bottom: none;
}

.topic-list li:hover {
  color: #4b6cb7;
  transform: translateX(5px);
}

.topic-tag {
  display: inline-block;
  color: #4b6cb7;
  font-weight: 600;
  margin-right: 5px;
}

.backtop-inner {
  height: 50px;
  width: 50px;
  background: linear-gradient(135deg, #4b6cb7 0%, #182848 100%);
  border-radius: 50%;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 24px;
  transition: all 0.3s ease;
  overflow: hidden;
  border: 2px solid rgba(255, 255, 255, 0.2);
}

.backtop-inner:hover {
  transform: translateY(-5px);
  box-shadow: 0 12px 25px rgba(0, 0, 0, 0.2);
  border-color: rgba(255, 255, 255, 0.4);
}

.backtop-inner i {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
  width: 100%;
}

@media screen and (max-width: 1200px) {
  .right {
    display: none;
  }
  
  .news_box {
    width: 95%;
    padding: 0 10px;
  }
}

@media screen and (max-width: 768px) {
  .news_box {
    flex-direction: column;
    margin: 60px auto;
  }
  
  .left {
    width: 100%;
    margin-bottom: 20px;
    position: static;
  }
  
  .main {
    width: 100%;
  }
}

/* 添加新的样式 */
.section-title {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: 600;
  color: #4b6cb7;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(230, 235, 245, 0.8);
}

.section-title i {
  margin-right: 8px;
}

.user-activity {
  margin-top: 25px;
  padding-top: 20px;
  border-top: 1px solid rgba(230, 235, 245, 0.8);
}

.calendar-preview {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
}

.calendar-day {
  display: flex;
  flex-direction: column;
  align-items: center;
}

.day-circle {
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: #f5f7fa;
  color: #666;
  font-size: 14px;
  transition: all 0.3s;
}

.day-circle.active {
  background-color: #4b6cb7;
  color: white;
  box-shadow: 0 4px 10px rgba(75, 108, 183, 0.3);
}

.activity-info {
  text-align: center;
  font-size: 14px;
  color: #666;
}

.highlight {
  color: #4b6cb7;
  font-weight: 600;
}

.recommended-users {
  background: white;
  border-radius: 16px;
  padding: 25px;
  margin-top: 20px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(230, 235, 245, 0.8);
  transition: all 0.3s ease;
}

.recommended-users:hover {
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.12);
}

.user-list {
  display: flex;
  flex-direction: column;
  gap: 15px;
}

.user-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid rgba(230, 235, 245, 0.8);
}

.user-item:last-child {
  border-bottom: none;
}

.user-info-mini {
  flex: 1;
  margin-left: 10px;
}

.user-name {
  font-size: 14px;
  font-weight: 500;
  color: #333;
}

.user-desc {
  font-size: 12px;
  color: #999;
}

.system-notice {
  background: white;
  border-radius: 16px;
  padding: 25px;
  margin-bottom: 25px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  border: 1px solid rgba(230, 235, 245, 0.8);
  transition: all 0.3s ease;
}

.system-notice:hover {
  box-shadow: 0 15px 40px rgba(0, 0, 0, 0.12);
}

.notice-content {
  background: linear-gradient(135deg, rgba(75, 108, 183, 0.05) 0%, rgba(24, 40, 72, 0.05) 100%);
  padding: 15px;
  border-radius: 12px;
  font-size: 14px;
  color: #555;
  line-height: 1.6;
}

.notice-content p {
  margin: 5px 0;
}

.notice-time {
  text-align: right;
  font-size: 12px;
  color: #999;
  margin-top: 10px;
}
</style>

